# @File    : logUtil.py
# @Project : web_learn
import logging
import os
from logging.handlers import TimedRotatingFileHandler


class Logging:
    log_path = os.path.abspath(os.path.dirname(os.path.dirname(__file__))) + "\\" + 'logs'
    log_name = 'run' + ''.join('.log')

    def create_log(self, logger_name=log_name):
        self.logger = logging.getLogger(logger_name)
        self.logger.setLevel(logging.INFO)

        if not self.logger.handlers:
            """1.创建日志文件路径"""
            self.file_log_path = self.log_path + "\\" + self.log_name
            self.file_hander = TimedRotatingFileHandler(filename=self.file_log_path, when="D", inteval=1,
                                                        backupCount=1, delay=True, encode="utf-8")
            self.file_hander.formatter = logging.Formatter(
                '[%(asctime)s] - [%(levelname)s] - [%(lineno)d]: %(message)s', datefmt='%Y-%m-%d %H-%M-%S')
            self.logger.addHandler(self.file_hander)
            return self.logger


def info(msg):
    Logging().create_log().info(msg)


def debug(msg):
    Logging().create_log().debug(msg)


def warning(msg):
    Logging().create_log().warning(msg)
